//
//  Pet_RecordManager.swift
//  PetApp
//
//  Created by mac on 2023/9/20.
//  Copyright © 2023 Pet_. All rights reserved.
//

import UIKit

class Pet_RecordManager: NSObject {

    static let shared = Pet_RecordManager()
    
    func saveRecord(_ data: InfoUtil.recordDta) {
        let recordStr = MMKV.default()?.string(forKey: USEINFOMODEL.id ?? "" + "record")
        var models = recordStr?.kj.modelArray(InfoUtil.recordDta.self) ?? []
        models.insert(data, at: 0)
        let jsonData = models.kj.JSONString()
        MMKV.default()?.set(jsonData, forKey: USEINFOMODEL.id ?? "" + "record")
    }
    
    func RecordList() -> [InfoUtil.recordDta] {
        let messageStr = MMKV.default()?.string(forKey: USEINFOMODEL.id ?? "" + "record")
        let models = messageStr?.kj.modelArray(InfoUtil.recordDta.self) ?? []
        return models
    }

    func removeRecord(_ data: InfoUtil.recordDta) {
        /// 删除会话列表
        let recordStr = MMKV.default()?.string(forKey: USEINFOMODEL.id ?? "" + "record")
        var models = recordStr?.kj.modelArray(InfoUtil.recordDta.self) ?? []
        models.removeAll(where: {$0.kj.JSONString() == data.kj.JSONString()})
        let jsonData = models.kj.JSONString()
        MMKV.default()?.set(jsonData, forKey: USEINFOMODEL.id ?? "" + "record")
    }

    func doneRecord(_ data: InfoUtil.recordDta) {
        /// 删除会话列表
        let recordStr = MMKV.default()?.string(forKey: USEINFOMODEL.id ?? "" + "record")
        var models = recordStr?.kj.modelArray(InfoUtil.recordDta.self) ?? []
        models.removeAll(where: {$0.kj.JSONString() == data.kj.JSONString()})
        var model = data
        model.done = "1"
        models.insert(model, at: 0)
        let jsonData = models.kj.JSONString()
        MMKV.default()?.set(jsonData, forKey: USEINFOMODEL.id ?? "" + "record")
    }

}


class Pet_CollectManager: NSObject {
    static let shared = Pet_CollectManager()
    func saveRecord(_ data: InfoUtil.collectDta) {
        let recordStr = MMKV.default()?.string(forKey: USEINFOMODEL.id ?? "" + "collect")
        var models = recordStr?.kj.modelArray(InfoUtil.collectDta.self) ?? []
        models.insert(data, at: 0)
        let jsonData = models.kj.JSONString()
        MMKV.default()?.set(jsonData, forKey: USEINFOMODEL.id ?? "" + "collect")
    }
    
    func CollectList() -> [InfoUtil.collectDta] {
        let messageStr = MMKV.default()?.string(forKey: USEINFOMODEL.id ?? "" + "collect")
        let models = messageStr?.kj.modelArray(InfoUtil.collectDta.self) ?? []
        return models
    }
    
    func deleteRecord(_ data: InfoUtil.collectDta) {
        let recordStr = MMKV.default()?.string(forKey: USEINFOMODEL.id ?? "" + "collect")
        var models = recordStr?.kj.modelArray(InfoUtil.collectDta.self) ?? []
        models.removeAll(where: {$0.id == data.id})
        let jsonData = models.kj.JSONString()
        MMKV.default()?.set(jsonData, forKey: USEINFOMODEL.id ?? "" + "collect")
    }

}
